<template>
  <div :class="[prefixCls, { fullscreen }]">
    <Upload
      name="file"
      @change="handleChange"
      :action="uploadUrl"
      :showUploadList="false"
      accept=".jpg,.jpeg,.gif,.png,.webp"
      :custom-request="customRequest"
    >
      <a-button type="primary" v-bind="{ ...getButtonProps }">
        {{ t('component.upload.imgUpload') }}
      </a-button>
    </Upload>
  </div>
</template>
<script lang="ts" setup>
import {computed} from 'vue';

import {Upload} from 'ant-design-vue';
  import { useDesign } from '@/hooks/web/useDesign';
  import { useGlobSetting } from '@/hooks/setting';
  import { useI18n } from '@/hooks/web/useI18n';
  import {UploadRequestOption} from "ant-design-vue/lib/vc-upload/interface";
  import {isFunction} from "@/utils/is";
  import {warn} from "@/utils/log";
  import {uploadContainerProps} from "@/components/Upload/src/props";

  defineOptions({ name: 'TinymceImageUpload' });

  const props = defineProps({
    fullscreen: {
      type: Boolean,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    ...uploadContainerProps,
  });

async function customRequest(info: UploadRequestOption<any>) {
    const { api } = props;
    if (!api || !isFunction(api)) {
      return warn('upload api must exist and be a function');
    }
    try {
      const res = await props.api?.({
        data: {
          ...(props.uploadParams || {}),
        },
        file: info.file,
        name: props.name,
        filename: props.filename,
      });
      info.onSuccess!({
        data:res
      });
    } catch (e: any) {
      info.onError!(e);
    }
  }


  const emit = defineEmits(['uploading', 'done', 'error']);

  let uploading = false;

  const { uploadUrl } = useGlobSetting();
  const { t } = useI18n();
  const { prefixCls } = useDesign('tinymce-img-upload');

  const getButtonProps = computed(() => {
    const { disabled } = props;
    return {
      disabled,
    };
  });

  function handleChange(info: Record<string, any>) {
    const file = info.file;
    const status = file?.status;
    const url = file?.response?.data;
    const name = file?.name;

    if (status === 'uploading') {
      if (!uploading) {
        emit('uploading', name);
        uploading = true;
      }
    } else if (status === 'done') {
      emit('done', name, url);
      uploading = false;
    } else if (status === 'error') {
      emit('error');
      uploading = false;
    }
  }
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-tinymce-img-upload';

  .@{prefix-cls} {
    position: absolute;
    z-index: 20;
    top: 4px;
    right: 10px;

    &.fullscreen {
      position: fixed;
      z-index: 10000;
    }
  }
</style>
